﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OOLib
{
   /// <summary>
   /// Is used to access named objects within a container. 
   /// </summary>
   /// <remarks>
   /// To implement inaccurate name access, support the ::com::sun::star::beans::XExactName interface.
   /// </remarks>
   public class NameAccess : ElementAccess
   {
      #region Constructors

      /// <summary>
      /// Constructor
      /// </summary>
      /// <remarks>
      /// After using this constructor, the ComObj must be set manually!
      /// </remarks>
      public NameAccess() :
         base()
      { }

      /// <summary>
      /// Constructor
      /// </summary>
      /// <param name="comObject">COM object</param>
      public NameAccess(object comObject) :
         base(comObject)
      { }

      #endregion

      /// <summary>
      /// Gets the object with the specified name.
      /// </summary>
      /// <param name="name">The name of the object.</param>
      /// <returns>Returns the object with the specified name.</returns>
      /// <remarks>!!! Not tested !!!</remarks>
      public object GetByName(string name)
      {
         CheckDisposed();
         return InvokeMethod("getByName", name);
      }

      /// <summary>
      /// Gets a sequence of all element names in this container.
      /// </summary>
      /// <returns>Returns a sequence of all element names in this container.</returns>
      /// <remarks>
      /// The order of the names is not specified.
      /// !!! Not tested !!!
      /// </remarks>
      public string[] GetElemetNames()
      {
         CheckDisposed();
         return (string[])InvokeMethod("getElementNames");
      }

      /// <summary>
      /// Gets if an element with this name is in the container.
      /// </summary>
      /// <param name="name">The name of the object.</param>
      /// <returns>Returns true if an element with this name is in the container, false otherwise.</returns>
      /// <remarks>!!! Not tested !!!</remarks>
      public bool HasByName(string name)
      {
         CheckDisposed();
         return (bool)InvokeMethod("hasByName", name);
      }

      /// <summary>
      /// Gets the object with the specified name.
      /// </summary>
      /// <param name="name">The name of the object.</param>
      /// <returns>Returns the object with the specified name.</returns>
      public object this[string name]
      {
         get 
         {
            CheckDisposed();
            return GetByName(name); 
         }
      }
   }

   /// <summary>
   /// Is used to access named objects of a specific type within a container. 
   /// </summary>
   public class NameAccess<T> : NameAccess where T : ComObjectWrapper, new()
   {
      #region Constructors

      /// <summary>
      /// Constructor
      /// </summary>
      /// <remarks>
      /// After using this constructor, the ComObj must be set manually!
      /// </remarks>
      public NameAccess() :
         base()
      { }

      /// <summary>
      /// Constructor
      /// </summary>
      /// <param name="comObject">COM object</param>
      public NameAccess(object comObject) :
         base(comObject)
      { }

      #endregion

      /// <summary>
      /// Gets the object with the specified name.
      /// </summary>
      /// <param name="name">The name of the object.</param>
      /// <returns>Returns the object with the specified name.</returns>
      public new T GetByName(string name)
      {
         return GetObjectWrapper<T>(base.GetByName(name));
      }

      /// <summary>
      /// Gets the object with the specified name.
      /// </summary>
      /// <param name="name">The name of the object.</param>
      /// <returns>Returns the object with the specified name.</returns>
      public new T this[string name]
      {
         get { return this.GetByName(name); }
      }
   }
}
